<template>
  <div class="app-container">
    <div class="op-container">
      <k-button v-if="authorityHandle(49)" class="top-btn" value="添加分类" @click.native="addGiftCategoryDialog" />
      <!--表格相关 -->
      <el-table
        ref="dragTable"
        :data="prizeTypeDataList"
        fit
        border
        row-key="id"
        highlight-current-row
        style="width: 100%;border-radius:8px;"
        :header-cell-style="{background:'#F6F5FA',color:'#4E4F62'}"
      >
        <el-table-column type="index" label="序号" width="60px" align="center" />
        <el-table-column min-width="150px" label="分类图标" align="center">
          <template slot-scope="scope">
            <el-image
              style="width: 110px; height: 80px"
              :src="scope.row.iconUrl"
              :preview-src-list="[scope.row.iconUrl]"
              fit="contain"
            />
          </template>
        </el-table-column>
        <el-table-column prop="typeName" min-width="150px" label="分类名称" align="center" show-overflow-tooltip />
        <el-table-column prop="superName" min-width="150px" label="父级分类" align="center" show-overflow-tooltip />
        <el-table-column min-width="150px" label="操作" align="center" v-if="authorityHandle(49)">
          <template slot-scope="scope">
            <div class="op-div">
              <k-button class="table-btn" value="修改" @click.native="addBoxUpdateDialog(scope.row)" />
              <k-button class="table-btn" value="删除" @click.native="deletGiftCategory(scope.row.id)" />
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加箱子 弹窗 -->
      <k-dialog
        :show-hide.sync="showAddGiftCategory"
        :modal-append-to-body="false"
        :append-to-body="true"
        :title="textMap[dialogStatus]"
        class="addBox-dialog"
        :show-foot="false"
        :two-btn="true"
        @click="savePrizeTypeData"
      >
        <el-form :model="prizeTypeModel" label-width="auto" class="form-modify">
          <el-form-item label="父级分类：">
            <el-select v-model="prizeTypeModel.superTypeId" clearable placeholder="请选择">
              <el-option
                v-for="item in superPrizeTypeList"
                :key="item.lineId"
                :label="item.lineName"
                :value="item.lineId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="分类名称：">
            <el-input v-model="prizeTypeModel.typeName" show-word-limit placeholder="请输入分类名称" />
          </el-form-item>
          <el-form-item label="分类图标：" prop="iconUrl">
            <k-upload-file
              ref="upload1"
              v-model="prizeTypeModel.iconUrl"
              upload-style="img"
              @successUpload="successUpload($event)"
            />
            <span style="color: #b2b2b2">只能上传jpg/png文件，且不超过2M</span>
          </el-form-item>
        </el-form>
      </k-dialog>
    </div>
  </div>
</template>

<script>
import { listPrizeTypeData, getSuperPrizeTypeList, savePrizeTypeData, updatePrizeTypeData, updateTypeSort, deletePrizeTypeData } from '@/api/task/index'
import Sortable from 'sortablejs'
export default {
  data() {
    return {
      ruleGuideContent: {
        content: ''
      },
      ruleGuide: {
        showHide: false
      },
      listQuery: {
        pager: {
          pageNo: 1,
          pageSize: 100,
          totalRows: '',
          start: ''
        },
        sort: '', // 排序字段 为对应列名,
        direction: '' // 排序 正向(asc)逆向(desc),
      },
      gradeOptions: [],
      prizeTypeModel: {
        id: undefined,
        superTypeId: undefined,
        typeName: '',
        iconUrl: ''
      },
      dialogStatus: 'create',
      textMap: {
        update: '修改分类',
        create: '创建分类'
      },
      showAddGiftCategory: false,
      gradeSettingModel: {
        gradeList: [{
          id: undefined,
          grade: undefined,
          minRecharge: '',
          maxRecharge: ''
        }],
        decayPrice: ''
      },
      gradeSetting: {
        showHide: false
      },
      prizeTypeDataList: [],
      superPrizeTypeList: [],
      sortable: null
    }
  },
  created() {
    this.getList()
    getSuperPrizeTypeList().then(res => {
      this.superPrizeTypeList = res.data
    })
  },
  methods: {
    // 等级箱子列表页加载数据
    getList() {
      listPrizeTypeData(this.listQuery).then(res => {
        this.prizeTypeDataList = res.data.rows
        this.$nextTick(() => {
          this.setSort()
        })
      })
    },
    setSort() {
      const el = this.$refs.dragTable.$el.querySelectorAll('.el-table__body-wrapper > table > tbody')[0]
      this.sortable = Sortable.create(el, {
        ghostClass: 'sortable-ghost', // Class name for the drop placeholder,
        setData: function(dataTransfer) {
          // to avoid Firefox bug
          dataTransfer.setData('Text', '')
        },
        onEnd: evt => {
          const targetRow = this.prizeTypeDataList.splice(evt.oldIndex, 1)[0]
          this.prizeTypeDataList.splice(evt.newIndex, 0, targetRow)
          this.prizeTypeDataList.map((item, index) => {
            item.typeSort = index
          })
          // 等级箱子列表页更新排序
          updateTypeSort(this.prizeTypeDataList).then(res => {
            this.$log(res)
          })
        }
      })
    },

    addGiftCategoryDialog() {
      this.dialogStatus = 'create'
      this.showAddGiftCategory = true
      this.$resetForm(this.prizeTypeModel)
    },
    addBoxUpdateDialog(row) {
      this.dialogStatus = 'update'
      this.$resetForm(this.prizeTypeModel)
      this.prizeTypeModel.id = row.id
      this.prizeTypeModel.superTypeId = row.superTypeId
      this.prizeTypeModel.typeName = row.typeName
      this.prizeTypeModel.iconUrl = row.iconUrl
      this.showAddGiftCategory = true
    },
    deletGiftCategory(boxid) {
      this.$confirm('确定要删除该分类么？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePrizeTypeData({ 'id': boxid }).then(res => {
          if (res.data.success) {
            this.$message.success('操作成功')
            this.getList()
          } else {
            this.$message.error(res.data.msg)
          }
        })
      }).catch(() => {

      })
    },
    // 添加分类
    savePrizeTypeData() {
      this.$log(this.prizeTypeModel)
      if (this.dialogStatus === 'create') {
        savePrizeTypeData(this.prizeTypeModel).then(res => {
          if (res.data.success) {
            this.$resetForm(this.prizeTypeModel)

            this.$message.success('操作成功')
            this.getList()
          } else {
            this.$message.error(res.data.msg)
          }
        })
      } else {
        updatePrizeTypeData(this.prizeTypeModel).then(res => {
          if (res.data.success) {
            this.$resetForm(this.prizeTypeModel)
            this.$message.success('操作成功')
            this.getList()
          } else {
            this.$message.error(res.data.msg)
          }
        })
      }
      this.showAddGiftCategory = false
    },
    successUpload(data) {
      this.prizeTypeModel.iconUrl = data
    }
  }
}
</script>
<style>
.sortable-ghost{
  opacity: .8;
  color: #fff!important;
  background: #3b425d!important;
}
</style>
<style lang="scss" scoped>
$mainColor:#3b425d;// 主色调
$disabledColor: #909399; //禁用色
$hoverColor:#1D143C;// 鼠标停留主色调浅色

.op-container{
  margin: 20px;
}
.top-btn{
  margin: 10px 0px 30px 0px;
  font-size: 14px;
  color: #ffffff;
  &:nth-of-type(2){
    margin-left: 10px;
  }
}
.top-right-btn{
  position: absolute;
  right: 20px;
  top: 30px;
  ::v-deep .el-button{
    background-color: #F8F7FB;
    border-color: transparent;
    color: #14162E;
    &:hover{
      color: #ffffff;
      background-color: $mainColor;
    }
    &:active{
      color: #14162E;
    }
  }
}
::v-deep .el-select .el-input.is-focus .el-input__inner {
    border-color: $mainColor;
}
::v-deep .el-input__inner:focus {
    border-color: $mainColor;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.price-input{
  width: 70px;
}
.addBox-dialog{
  ::v-deep .el-dialog__body{
    padding: 10px 80px;
  }

 ::v-deep .image-slot {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    background: #f5f7fa;
    color: #909399;
    font-size: 40px;
  }
}
.upload-btn {
  font-size: 14px;
  color: #ffffff;
}
.editer {
  border-radius: 8px;
  float: left;
  box-sizing: border-box;
  color: #606266;
  ::v-deep .ql-container {
    height: 440px;
    border-radius: 0px 0px 8px 8px;
  }
  ::v-deep .ql-toolbar.ql-snow {
    border-radius: 8px 8px 0px 0px;
  }
}

.dialog-warningSetting{
  ::v-deep.el-input{
    width: 110px;
  }
  .price-input{
    width: 100px;
  }
  .el-icon{
    &-circle-plus, &-remove{
      font-size: 22px;
      color:#3b425d;
      margin-left: 10px;

   }
  }
  ul {
    list-style-type:none;
    margin-top: 20px;
    padding:0;
    li{
      margin: 0 0 30px 0px;
      display: flex;
      flex-direction: row;
      align-items: center;
      i{
        color: #D1D5DD;
      }
    }
  }
  .warning-title {
      font-size: 16px;
      color: #14162E;
      &-right{
        font-size: 16px;
        color: #14162E;
        margin-left: 80px;
      }
      &-right2{
        font-size: 16px;
        color: #14162E;
        margin-left: 20px;
      }
    }
}
.table-btn{
  width: 90px;
  height: 35px;
  line-height:35px;
  padding: 0;
  background: #e7ecf8;
  border: 1px solid $mainColor;
  border-radius: 8px;
  font-size: 14px;
  color: $mainColor;
  &:hover{
    background: $mainColor;
    color: #fff;
  }
   &:focus {
     background-color: #e7ecf8;
  }
}

</style>
